package com.moli.iov.down.command.design.factory;

import com.moli.iov.down.command.design.command.retry.RetryCommand;
import com.moli.iov.down.command.design.command.retry.impl.GeneralCommand;
import com.moli.iov.down.command.design.command.retry.impl.UpgradeCommand;
import com.moli.iov.down.enums.command.CommandEnum;
import com.moli.iov.util.ByteUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  终端协议重发机制 处理业务对象 工厂
 * </p>
 *
 * @author: zql
 * @Date: 2019/1/9 14:36
 */
public class RetryCommandFactory {

    private static  final Logger logger = LoggerFactory.getLogger(RetryCommandFactory.class);

    private static final Map<Byte, RetryCommand> RETRY_COMMAND_MAP = new HashMap<>();

    static{
        RETRY_COMMAND_MAP.put(CommandEnum.REMOTE_CONTROL.getFlag(), new GeneralCommand());
        RETRY_COMMAND_MAP.put(CommandEnum.TERMINAL_PARAMETER_QUERY.getFlag(), new GeneralCommand());
        RETRY_COMMAND_MAP.put(CommandEnum.TERMINAL_PARAMETER_SETTING.getFlag(), new GeneralCommand());
        RETRY_COMMAND_MAP.put(CommandEnum.TERMINAL_CONTROL.getFlag(), new GeneralCommand());
        RETRY_COMMAND_MAP.put(CommandEnum.REMOTE_ROLL_CALL.getFlag(), new GeneralCommand());
        RETRY_COMMAND_MAP.put(CommandEnum.REMOTE_UPGRADE.getFlag(), new UpgradeCommand());
    }

    private RetryCommandFactory(){}

    public static RetryCommand getRetryCommand(Byte command){
        RetryCommand retryCommand = RETRY_COMMAND_MAP.get(command);
        if(retryCommand == null){
            String commandStr = ByteUtils.toHexString(command);
            logger.error("生成 终端协议重发机制 处理业务对象 失败 未定义指令 命令：{}", commandStr);
        }
        return retryCommand;
    }
}
